package com.dycong.common.leetcode.yi.ThreadSort;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yilin
 * Date: 2019-09-06
 * Time: 12:22
 */
public class ThreadCondtionSort {

    private static Lock lock = new ReentrantLock();
    private static Condition condition1 = lock.newCondition();
    private static Condition condition2 = lock.newCondition();

    private static Boolean t1Run = false;
    private static Boolean t2Run = false;

    public static void main(String[] args) {
        final Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                t1Run = true;
                System.out.println("产品经理规划新需求");
                condition1.signal();
                lock.unlock();
            }
        });

        final Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                if (!t1Run) {
                    try {
                        System.out.println("开发人员先休息会...");
                        condition1.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("开发人员开发新需求功能");
                t2Run = true;
                condition2.signal();
                lock.unlock();
            }
        });


        final Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                if (!t2Run) {
                    try {
                        System.out.println("测试人员先休息会...");
                        condition2.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("测试人员测试新功能");
                lock.unlock();
            }
        });


        System.out.println("早上：");
        System.out.println("测试人员来上班了...");
        thread3.start();
        System.out.println("产品经理来上班了...");
        thread1.start();
        System.out.println("开发人员来上班了...");
        thread2.start();
    }
}
